package com.gxuwz.smem.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcUtils {
	private static final String url = "jdbc:mysql://localhost:3306/stumoraledumanage";
	private static final String user = "root";
	private static final String password = "123456";

	/**
	 * 连接MySQL数据库，返回一个连接
	 * 
	 * @return
	 */
	public static Connection getConnection() {

		Connection conn = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void main(String[] args) {
		try {
			// 实例JdbcUtils化对象
			JdbcUtils jdbcUtils = new JdbcUtils();
			Connection conn = jdbcUtils.getConnection();
			System.out.println("连接对象是否为Null?" + conn.getCatalog());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public static void close(Connection conn, Statement stmt, ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
			if (stmt != null) {
				stmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public static void close(Connection conn, Statement stmt) {
		close(conn, stmt, null);
	}
}
